/*-----------------------------------------------------------------------------------
 *
 * 프로그램명	: BoardDAO.java
 * Version		: 1.0
 * 작성일 		: 2006-08-16
 * 작성자		:
 * 수정일 		:
 * 수정자		:
 * 설명		 	:
 * 기능		 	:
 *
 * Copyright (c) 2000 by H&H  All Rights Reserved.
 *
-------------------------------------------------------------------------------------*/
package com.joinhawaii.home;

import java.io.*;

import java.util.*;
import java.sql.*;

import com.zll.framework.db.DBManager;
import com.zll.framework.model.HashList;
import com.zll.framework.util.StringUtil;


public class BoardDAO
{

	public final int LINE_PER_PAGE	= 15;

	public int getTotalPage(int totalCount) {
		return ((totalCount - 1) / this.LINE_PER_PAGE) + 1;
	}

	public HashList selectBoardList(HashMap infos) throws Exception
	{
        return DBManager.executeQuery("/board.xml", "select_board_list", infos);
	}

	public HashList selectWordList(HashMap infos) throws Exception
	{
		int currPage	= Integer.parseInt((String)infos.get("curr_page"));
		int startNum	= (currPage - 1) * this.LINE_PER_PAGE;
		int endNum		= this.LINE_PER_PAGE;

		infos.put("start_num",	new Integer(startNum));
		infos.put("end_num",	new Integer(endNum));

		return DBManager.executeQuery("/board.xml", "select_word_list", infos);
	}

	public HashList selectWordDetail(HashMap infos) throws Exception
	{
        return DBManager.executeQuery("/board.xml", "select_word_detail", infos);
	}

	public int selectWordCount(HashMap infos) throws Exception
	{
        HashList list = DBManager.executeQuery("/board.xml", "select_word_count", infos);
		return Integer.parseInt(list.get("word_count",0));
	}

	public String selectWordNum(String board_id) throws Exception
	{
		HashMap infos = new HashMap();
		infos.put("board_id",	board_id);
        HashList list = DBManager.executeQuery("/board.xml", "select_word_num", infos);
		return StringUtil.nvl(list.get("word_num",0));
	}


	public HashList selectComment(HashMap infos) throws Exception
	{
        return DBManager.executeQuery("/board.xml", "select_comment", infos);
	}






    /**
	 * 게시글 저장
	 *
	 * @param	infos
	 * @return	boolean
	 */
	public boolean saveWord(HashMap infos)
    {
		Connection conn = null;
        boolean result	= true;

        try
		{
			conn = DBManager.getConnection();

			String job_type = StringUtil.nvl(infos.get("job_type"));

	    	if (job_type.equals("C") || job_type.equals("R"))
	    	{
	    		DBManager.executeUpdate("/board.xml", "insert_word", infos);
	       	}
	       	else if (job_type.equals("U"))
	       	{
	       		DBManager.executeUpdate("/board.xml", "update_word", infos);
	       	}
	       	else if (job_type.equals("D"))
	       	{
	       		DBManager.executeUpdate("/board.xml", "delete_word", infos);
	        }
	        else if (job_type.equals("UC")) 	// 조회수 증가
	        {
	        	DBManager.executeUpdate("/board.xml", "update_word_count", infos);
	        }
	    }
	    catch(Exception e)
	    {
	    	result 	= false;
	    	e.printStackTrace();
		}
	    finally
	    {
	    	DBManager.closeConnection(conn);
		}

        return result;
    }



    /**
	 * 댓글 저장
	 *
	 * @param	infos
	 * @return	boolean
	 */
	public boolean saveComment(HashMap infos)
    {
		Connection conn = null;
        boolean result	= true;

        try
		{
			conn = DBManager.getConnection();

			conn.setAutoCommit(false);

			ArrayList dataList = (ArrayList)infos.get("dataList");

		    for (int i = 0; i < dataList.size(); i++)
		    {
				HashMap map = (HashMap)dataList.get(i);

		    	if ("C".equals(map.get("job_type")))
		    	{
		    		DBManager.executeUpdate("/board.xml", "insert_comment", map, conn);
		       	}
		       	else if ("U".equals(map.get("job_type")))
		       	{
		       		DBManager.executeUpdate("/board.xml", "update_comment", map, conn);
		       	}
		       	else if ("D".equals(map.get("job_type")))
		       	{
		       		DBManager.executeUpdate("/board.xml", "delete_comment", map, conn);
		        }
			}

			conn.commit();
	    }
	    catch(Exception e)
	    {
	    	try {
	    		conn.rollback();
	    	} catch (Exception ce) {
	    		//
	    	}
	    	result 	= false;
	    	e.printStackTrace();
		}
	    finally
	    {
	    	try {
	    		conn.setAutoCommit(true);
	    	} catch (Exception se) {
	    		//
	    	}
	    	DBManager.closeConnection(conn);
		}

        return result;
    }



}
